#!/usr/bin/env bash

#一键运行脚本

#run me:
#echo 'usage:
#mkdir work_root && cd work_root
#echo "/app/miniconda3/bin/python"> ./conda_python
#echo "/app/miniconda3/bin:/app/miniconda3/condabin"> ./conda_env_path
#echo "/pubx/8cc/8cc" > ./cmd ;
#echo "8cc" > ./short_process_name ;
#echo "/pubx/8cc/8cc -c /pubx/8cc/mini_main.c" > ./cmd_with_args ;
#bash -x /pubx/hook_mem_alloc/run.sh  conda_python conda_env_path cmd short_process_name cmd_with_args
##############
#mkdir work_root && cd work_root
#echo "/app/miniconda3/bin/python"> ./conda_python
#echo "/app/miniconda3/bin:/app/miniconda3/condabin"> ./conda_env_path
#echo "/usr/bin/x86_64-linux-gnu-gcc-11" > ./cmd ;
#echo "x86_64-linux-gnu-gcc-11" > ./short_process_name ;
#echo "/usr/bin/x86_64-linux-gnu-gcc-11 -c /pubx/8cc/mini_main.c" > ./cmd_with_args ;
#bash -x /pubx/hook_mem_alloc/run.sh conda_python conda_env_path cmd short_process_name cmd_with_args
#'

if [ "$#" -lte 6 ]; then
    echo 'mkdir work_root && cd work_root
          echo "/app/miniconda3/bin/python"> ./conda_python
          echo "/app/miniconda3/bin:/app/miniconda3/condabin"> ./conda_env_path
          echo "/usr/bin/x86_64-linux-gnu-gcc-11" > ./cmd ;
          echo "x86_64-linux-gnu-gcc-11" > ./short_process_name ;
          echo "/usr/bin/x86_64-linux-gnu-gcc-11 -c /pubx/8cc/mini_main.c" > ./cmd_with_args ;

          bash -x /pubx/hook_mem_alloc/run.sh conda_python conda_env_path cmd short_process_name cmd_with_args
          '
    exit 99
fi

P_conda_python=$1
P_conda_env_path=$2
P_cmd=$3
P_short_process_name=$4
P_cmd_with_args=$5

#{read param
conda_env_path="`cat ${P_conda_env_path}`"
PATH=conda_env_path:$PATH
#conda_env_path: /app/miniconda3/bin:/app/miniconda3/condabin

PYTHON="`cat ${P_conda_python}`"
#PYTHON: /app/miniconda3/bin/python

cmd_with_args="`cat ${P_cmd_with_args}`"
cmd="`cat ${P_cmd}`"
cmdBaseName=`basename $cmd`
workHome="`pwd`/hma_workHome/${cmdBaseName}_`date +%s`"

short_process_name="`cat ${P_short_process_name}`"

#}

rm -fr $workHome && mkdir -p $workHome && cd $workHome && \

cd /pubx/hook_mem_alloc/  && bash build.sh && cd - && rm -fr /pubx/hook_mem_alloc/*.out &&  \
ls /pubx/hook_mem_alloc/libHookMemAlloc.so && \

LD_PRELOAD=/pubx/hook_mem_alloc/libHookMemAlloc.so $cmd_with_args && \
f_MemAllocLog_out="${workHome}/_MemAllocLog_${short_process_name}_*.out" && \
if ! test -f $f_MemAllocLog_out ; then  echo "没找到 ${f_MemAllocLog_out}, 可能是因为 libHookMemAlloc.so 中的保存日志 没有在该进程退出时触发 (即 libHookMemAlloc.so 中的 atexit(save) 不其作用）" && exit 98 ; fi && \
MemAllocLog="`ls ${workHome}/_MemAllocLog_${short_process_name}_*.out`" && \
soList="`ls ${workHome}/_soList_${short_process_name}_*.out`" && \

# 补调用者地址  前 停一下 给freecad这种每次运行时依赖路径不同的 应用 一次 用准备好的依赖路径假装此次依赖路径 的 机会
read -p "即将 补调用者地址(addrCsv2elfSym.py)： 若 应用 每次运行时依赖路径都不同， 请 确保 此次对应用加钩子运行时的依赖路径存在，再 回车以继续 补调用者地址 ；  否则，直接 回车以继续 补调用者地址" && \
pwd && sudo $PYTHON /pubx/hook_mem_alloc/addr2func/addrCsv2elfSym.py $cmd  $MemAllocLog  $soList && \
MemAllocLogNew=`ls ${workHome}/_MemAllocLog_${short_process_name}_*.out.new` && \


echo -n $MemAllocLogNew > ./MemAllocLogNew
cat  << 'EOF' > ./replace_MemAllocLog_Analyze.py
with open("./MemAllocLogNew","r") as f0:
  MemAllocLogNew=f0.read()
print(f"MemAllocLogNew:{MemAllocLogNew}")
with open("/pubx/hook_mem_alloc/example/MemAllocLog_Analyze.py","r") as fr:
  s=fr.read()
print(f"len(s):{len(s)}")
s=s.replace('##fn=ToReplaceMeByTerminalPython',f"fn='{MemAllocLogNew}'")
print(f"len(s):{len(s)}")
with open("./MemAllocLog_Analyze.py.tmp","w") as fw:
  fw.write(s)
EOF

$PYTHON ./replace_MemAllocLog_Analyze.py

#awk """{gsub('##fn=ToReplaceMeByTerminalPython', 'fn=\"$MemAllocLogNew\"', "\$0"); print}"""   /pubx/hook_mem_alloc/example/MemAllocLog_Analyze.py >  MemAllocLog_Analyze.py.tmp && \
$PYTHON MemAllocLog_Analyze.py.tmp

#sed "s/##fn=ToReplaceMeByTerminalPython/fn=\'$MemAllocLogNew\'/"  /pubx/hook_mem_alloc/example/MemAllocLog_Analyze.py  >  MemAllocLog_Analyze.py.tmp
#awk '{gsub("##fn=ToReplaceMeByTerminalPython", "fn=\"/pubx/hook_mem_alloc/hma_workHome/x86_64-linux-gnu-gcc-11_1685156510/_MemAllocLog_x86_64-linux-gnu-gcc-11_20230527_110150.out.new\"", $0); print}'   /pubx/hook_mem_alloc/example/MemAllocLog_Analyze.py